<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ page import="cn.firstsoft.firstframe.admin.PropertyFile"%>
<%@  page import="java.util.Map"%>
<%@  page import="java.util.HashMap"%>

<%@  page import="cn.firstsoft.modules.integration.utils.IntegratUtils"%>
<%@  page import="cn.firstsoft.firstframe.web.FirstFrameBeanFactory"%>
<%@  page import="cn.firstsoft.modules.integration.service.logic.IntegratSobManager"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
	IntegratSobManager integratSobManager = (IntegratSobManager)FirstFrameBeanFactory.getBean("integratSobManager");
	String idUserName = request.getParameter("idUserName");
	String msg = "";
	if(idUserName != null){
		String idPassword = request.getParameter("idPassword");
		String hrServerName = request.getParameter("K/3HRServerName");
		String k3WebServerName = request.getParameter("K/3WEBServerName");
		String srmServerName = request.getParameter("K/3SRMServerName");
		String biServerName = request.getParameter("K/3BIServerName");
		String plmServerName = request.getParameter("K/3PLMServerName");
		String plmWebServiceUrl = request.getParameter("plmWebServiceUrl");
		String plmLoginUrl = request.getParameter("plmLoginUrl");
		
		Map map = new HashMap();
		map.put("idUserName", idUserName);
		map.put("idPassword",idPassword);
		map.put("plm.webservice.url",plmWebServiceUrl);
		map.put("plm.login.url",plmLoginUrl);	
		map.put("K/3HR",hrServerName);
		map.put("K/3WEB",k3WebServerName);
		map.put("K/3SRM",srmServerName);
		map.put("K/3BI",biServerName);
		map.put("K/3PLM",plmServerName);
		
		IntegratUtils.saveIntegratConf(map);
		String[] ids = request.getParameterValues("id");
		String[] sysCodes = request.getParameterValues("sysCode");
		String[] sobIds= request.getParameterValues("sobId");
		String[] sobNames = request.getParameterValues("sobName");
		String[] dbUserNames = request.getParameterValues("dbUserName");
		String[] dbPasswords = request.getParameterValues("dbPassword");
		String[] dbUrls = request.getParameterValues("dbUrl");
		String[] dbDriverClasses = request.getParameterValues("dbDriverClass");
		
		msg = integratSobManager.saveSobs(ids, sysCodes, sobIds, sobNames, dbUserNames, dbPasswords, dbUrls, dbDriverClasses);
	}
%>
<c:set var="msg" value="<%=msg%>" />
<c:set var="m" value="<%=IntegratUtils.getIntegratConf()%>" />
<c:set var="sysSobs" value="<%=integratSobManager.sysSobs()%>" />

<html>
	<head>
		<title>管理控制台 - 环境设置 - 业务系统集成</title>
		<%@ include file="/common/meta2.jsp"%>
		<link href="../style.css" type="text/css" rel="stylesheet">
		<script language="javascript" src="../Check.js"></script>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		
	</head>
	<body>
		<form method="post" name="businessForm"
			action="sob_edit.jsp">
			<div>
				<input class="button" type="submit" value="提&nbsp;&nbsp;交" />
				<input class="button" type="reset" value="重&nbsp;&nbsp;置" />
			</div>
			${msg }
			<table>
				<tr>
					<td class="listViewPaginationTdS1" >
					<div width="100%">用户设置</div>
					</td>
			</tr>
			<tr>
				<td>
					<div>
					<table width="100%" align="center">
						<tr>
							<td class=dataLabel><a>PLM Webservice地址</a>
							</td>
							<td class=dataField><input name="plmWebServiceUrl" size="120" id=""plmWebServiceUrl"" value="${m['plm.webservice.url'] }"/>
							</td>
						</tr>
						<tr>
							<td class=dataLabel><a>PLM系统登录地址</a>
							</td>
							<td class=dataField><input  name="plmLoginUrl" size="120" id="plmLoginUrl" value="${m['plm.login.url'] }"/>
							</td>
						</tr>
						<tr>
							<td class=dataLabel><a>集成用户名称</a>
							</td>
							<td class=dataField><input name="idUserName"  id="idUserName" value="${m['idUserName'] }"/>
							</td>
						</tr>
						<tr>
							<td class=dataLabel><a>集成用户密码</a>
							</td>
							<td class=dataField><textarea name="idPassword"  cols="100" rows="3" id="idPassword" >${m['idPassword'] }</textarea>
							</td>
						</tr>
					</table>
					</div>
				</td>
			</tr>
			<c:set var="lastSysCode" value="-1"/> 
			<c:forEach items="${sysSobs }" var="sysSob" >
			<tr>
				<td class="listViewPaginationTdS1">
					<div  width="100%">${sysSob.key }
					<c:if test="${lastSysCode != '-1' }">
						<a href="javascript:copySob('${lastSysCode }','${sysSob.key }')" >帐套同上</a>
					</c:if>
					</div>
				</td>
			</tr>
			<tr>
				<td>
				<table>
				<c:if test="${!(sysSob.key=='K/3财务'||sysSob.key=='K/3供应链'||sysSob.key=='K/3生产制造')}">
					<tr><td class=dataLabel align="right" >应用名称:</td><td class=dataField align="left" colSpan=3><input type="text" name="${sysSob.key }ServerName"  id="_${sysSob.key }ServerName" value="${m[sysSob.key] }"/></td></tr>
				</c:if>
					<tr>
						<td class=dataLabel align="right" >帐套名称:</td><td class=dataField align="left" ><input type="text" name="_${sysSob.key }SobName"  id="_${sysSob.key }SobName" maxLength="50"/></td>
						<td class=dataLabel align="right" >帐套编码:</td><td class=dataField align="left" ><input type="text" name="_${sysSob.key }SobId"  id="_${sysSob.key }SobId" maxLength="5"/></td></tr>
					<tr>
						<td class=dataLabel align="right" >数据库用户:</td><td class=dataField align="left" ><input type="text" name="_${sysSob.key }DbUserName"  id="_${sysSob.key }DbUserName" maxLength="60"/></td>
						<td class=dataLabel align="right" >数据库密码:</td><td class=dataField align="left" ><input type="password" name="_${sysSob.key }DbPassword"  id="_${sysSob.key }DbPassword" maxLength="200"/></td></tr>
					<tr>
						<td class=dataLabel align="right" >数据库连接串:</td><td class=dataField align="left" colSpan=2><input type="text" name="_${sysSob.key }DbUrl"  id="_${sysSob.key }DbUrl" size="50" maxLength="200"/></td>
						<td class=dataField align="left" >示例:<br/>
						1、oracle：jdbc:oracle:thin:@127.0.0.1:1521:orcl<br/>
						2、Sqlserver：jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=AIS20110101000000
						</td></tr>
					<tr>
						<td class=dataLabel align="right" >数据库驱动类:</td><td class=dataField align="left" colSpan=2><input type="text" name="_${sysSob.key }DbDriverClass"  id="_${sysSob.key }DbDriverClass" size="50"  maxLength="200"/></td>
						<td class=dataField align="left" >示例:<br/>
						1、oracle：oracle.jdbc.driver.OracleDriver<br/>
						2、Sqlserver：net.sourceforge.jtds.jdbc.Driver
						</td></tr>
					<tr><td colSpan=4  align="center"><input class="button" type="button" value="添&nbsp;&nbsp;加" onclick="addSob('${sysSob.key }');"/></td></tr>
				</table>
				<div id="div${sysSob.key }">
				</div>
				<td>
			</tr>
			<c:set var="lastSysCode" value="${sysSob.key }"/>
			</c:forEach>
		</table>
		</form>
	</body>
	
	<script type="text/javascript">
		var sobs={};
		//把帐套信息放入div中，并会清空相关的text
		function addSob(sysCode){
			var sobName = $F("_"+ sysCode + "SobName");
			var sobId =  $F("_"+ sysCode + "SobId");
			var dbUserName =  $F("_"+ sysCode + "DbUserName");
			var dbPassword =  $F("_"+ sysCode + "DbPassword");
			var dbUrl =  $F("_"+ sysCode + "DbUrl");
			var dbDriverClass =  $F("_"+ sysCode + "DbDriverClass");

			if(sobName!=null)sobName = sobName.replace(/^\s+|\s+$/g,"");
			if(sobId!=null)sobId =  sobId.replace(/^\s+|\s+$/g,"");
			if(dbUserName!=null)dbUserName =  dbUserName.replace(/^\s+|\s+$/g,"");
			if(dbPassword!=null)dbPassword =  dbPassword.replace(/^\s+|\s+$/g,"");
			if(dbUrl!=null)dbUrl =  dbUrl.replace(/^\s+|\s+$/g,"");
			if(dbDriverClass!=null)dbDriverClass =  dbDriverClass.replace(/^\s+|\s+$/g,"");
			var id = "";
			
			if(add2Div(id, sysCode, sobName, sobId, dbUserName, dbPassword, dbUrl, dbDriverClass)){
				var txtSobName = $("_"+ sysCode + "SobName");
				txtSobName.value ="";
				txtSobName.focus();
				$("_"+ sysCode + "SobId").value ="";
				$("_"+ sysCode + "DbUserName").value ="";
				$("_"+ sysCode + "DbPassword").value ="";
				$("_"+ sysCode + "DbUrl").value ="";
				$("_"+ sysCode + "DbDriverClass").value ="";
				
			}
		}
		var plmAcctCount = 0;
		// 把帐套信息放入div中，会加入隐藏的sobName, sobId, sysCode  input对象
		function add2Div(id, sysCode, sobName, sobId, dbUserName, dbPassword, dbUrl, dbDriverClass){
			sobName=trim(sobName);
			sobId = trim(sobId);
			if(trim(sobName)=="" || trim(sobId)== ""){
				alert("帐套名称或编码不可为空!");
				return false ;
			}
			if (sobName.indexOf(" ") > 0) {
				alert("帐套名称中不能包含空格,请去掉空格!");
				return false;
			}
			if(!isLuhnNum(sobId)){
				alert("账套编码只能为数字!");
				return false;
			}
			if(document.getElementById(sysCode+sobName+'sobName')){
				alert( "已经存在帐套名称：" + sobName);
				return false;
			}
			if(document.getElementById(sysCode+sobId+'sobId')){
				alert( "已经存在帐套编码：" + sobId);
				return false;
			}
			if("K/3PLM"== sysCode ){
				if(plmAcctCount ==1){
					alert("PLM只有一个帐套!")
					return true;
				}
				plmAcctCount = 1;
			}
			addNew2Sobs(sysCode);
			sobs[sysCode].ids[sobName] = sobId;
			sobs[sysCode].names[sobName] = sobName;
			sobs[sysCode].dbUserNames[sobName] = dbUserName;
			sobs[sysCode].dbPasswords[sobName] = dbPassword;
			sobs[sysCode].dbUrls[sobName] = dbUrl;
			sobs[sysCode].dbDriverClasses[sobName] = dbDriverClass;
			var divParent = $("div"+ sysCode);
			var len = divParent.children.length;
			var newDivId= "div" + sysCode + sobId;
			var minusBar = "<img src='../../themes/mskin/images/sizeminus.gif' onclick=deleteSob('" + newDivId +"','" + sysCode + "','" + sobName + "') style='cursor:pointer;' />";
			var input = "<input type='hidden' name='id' value='" + id +"'/>";
			input = input + "<input type='hidden' id='"+sysCode+sobName+"sobName' name='sobName' value='" + sobName +"'/>";
			input = input + "<input type='hidden' id='"+sysCode+sobId+"sobId' name='sobId' value='" + sobId +"'/>";
			input = input + "<input type='hidden' name='sysCode' value='" + sysCode +"'/>";
			input = input + "<input type='hidden' name='dbUserName' value='" + dbUserName +"'/>";
			input = input + "<input type='hidden' name='dbPassword' value='" + dbPassword +"'/>";
			input = input + "<input type='hidden' name='dbUrl' value='" + dbUrl +"'/>";
			input = input + "<input type='hidden' name='dbDriverClass' value='" + dbDriverClass +"'/>";
			var newDiv = "<div id='" + newDivId  + "'>"+ input +  minusBar+"<a>帐套名称：" + sobName + "</a>&nbsp;&nbsp;<a>帐套编码： "+ sobId + "</a>&nbsp;&nbsp;<a>数据库用户：" + dbUserName + "</a>&nbsp;&nbsp;<a>数据库连接串：" + dbUrl + "</a>&nbsp;&nbsp;<a>数据库驱动：" + dbDriverClass + "</a>" +  "</div>" ;
			divParent.update(divParent.innerHTML +  newDiv);
			return true;
		}
		//如果sobs中不存在sysCode的sob，将建一个对象key为sysCode的sob集合
		function addNew2Sobs(sysCode){
			if(!sobs[sysCode]){
				sobs[sysCode] ={};
				sobs[sysCode].ids={};
				sobs[sysCode].names = {};
				sobs[sysCode].dbUserNames = {};
				sobs[sysCode].dbPasswords = {};
				sobs[sysCode].dbUrls = {};
				sobs[sysCode].dbDriverClasses = {};
			}
		}
		
		//删除帐套
		function deleteSob(divId, sysCode, sobName){
			if("K/3PLM"== sysCode){
				plmAcctCount = 0;
			}
			var div = $(divId);
			if(div){
				div.parentNode.removeChild(div);
			}
			if(sobs[sysCode] && sobs[sysCode].names[sobName]){
				sobs[sysCode].names[sobName] = null;
				sobs[sysCode].ids[sobName] = null;
				sobs[sysCode].dbUserNames[sobName] = null;
				sobs[sysCode].dbPasswords[sobName] = null;
				sobs[sysCode].dbUrls[sobName] = null;
				sobs[sysCode].dbDriverClasses[sobName] = null;
			}
		}
		
		//注意，如果目标的帐套中，帐套名和帐套编码只要有一个存在的话，就不会复制这个帐套
		function copySob(sysCodeSrc, sysCodeDest){
			if(sobs[sysCodeSrc]){
				addNew2Sobs(sysCodeDest);
				for(var sobName in sobs[sysCodeSrc].names){
					var sobName = sobs[sysCodeSrc].names[sobName];
					var sobId = sobs[sysCodeSrc].ids[sobName];
					var dbUserName = sobs[sysCodeSrc].dbUserNames[sobName];
					var dbPassword = sobs[sysCodeSrc].dbPasswords[sobName];
					var dbUrl = sobs[sysCodeSrc].dbUrls[sobName];
					var dbDriverClass = sobs[sysCodeSrc].dbDriverClasses[sobName];
					var id = "";

					var sobExisted =  sobs[sysCodeDest].names[sobName] || sobs[sysCodeDest].ids[sobId];
					if((!sobExisted) && sobId){
						add2Div(id, sysCodeDest, sobName, sobId, dbUserName, dbPassword, dbUrl, dbDriverClass);
					}
				}
			}
		}

		//初始化
		<c:forEach items="${sysSobs }" var="sysSob" >
			<c:forEach items="${sysSob.value }" var="sob" >
				add2Div("${sob.id}","${sysSob.key}","${sob.name}","${sob.sobId}","${sob.dbUserName}","${sob.dbPassword}","${sob.dbUrl}","${sob.dbDriverClass}");
			</c:forEach>
		</c:forEach>
		
		window.onerror  =function(l,s,d){
			alert("line: " + l + " d " + d);
		}
		
		//判断是否为数字      是:true 否:false
		function isLuhnNum(argvalue) {
            argvalue = argvalue.toString();
            if (argvalue.length == 0) {
                return false;
            }
            for (var n = 0; n < argvalue.length; n++) {
                if ((argvalue.substring(n, n+1) < "0") ||
                    (argvalue.substring(n,n+1) > "9")) {
                    return false;
                }
            }
            return true;
        }
	</script>
</html>
